package com.pxxy.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.pxxy.common.utils.R;
import com.pxxy.mall.product.dao.AttrAttrgroupRelationDao;
import com.pxxy.mall.product.dao.AttrDao;
import com.pxxy.mall.product.dto.attrgroup.AttrgroupReqDTO;
import com.pxxy.mall.product.entity.AttrAttrgroupRelationEntity;
import com.pxxy.mall.product.entity.AttrEntity;
import com.pxxy.mall.product.vo.AttrAttrgroupRelationVo;
import com.pxxy.mall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxxy.common.utils.PageUtils;
import com.pxxy.common.utils.Query;

import com.pxxy.mall.product.dao.AttrGroupDao;
import com.pxxy.mall.product.entity.AttrGroupEntity;
import com.pxxy.mall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao relationDao;


    @Autowired
    private AttrDao attrDao;


    @Autowired
    private AttrGroupDao attrGroupDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>());
        return new PageUtils(page);
    }


    @Override
    public List<AttrAttrgroupRelationVo> queryAttrRelation(Long attrGroupId) {
        List<AttrAttrgroupRelationVo> attrRelationVos = new ArrayList<>();
        List<AttrAttrgroupRelationEntity> entityList = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupId));
        entityList.stream().forEach(t -> {
            AttrAttrgroupRelationVo relationVo = new AttrAttrgroupRelationVo();
            //设置属性id
            Long attrId = t.getAttrId();
            relationVo.setAttrId(attrId);

            AttrEntity attrEntity = attrDao.selectOne(new QueryWrapper<AttrEntity>().lambda().eq(AttrEntity::getAttrId, attrId));
            //设置属性名
            relationVo.setAttrName(attrEntity.getAttrName());
            //设置可多选项
            relationVo.setValueSelect(attrEntity.getValueSelect());
            attrRelationVos.add(relationVo);
        });
        return attrRelationVos;
    }


    /**
     * 删除一条或多条数据
     *
     * @param attrgroupReqDTO
     */
    @Transactional
    @Override
    public void removeMoreByIds(List<AttrgroupReqDTO> attrgroupReqDTO) {
        List<Long> relationIds = new ArrayList<>();
        attrgroupReqDTO.stream().forEach(t -> {
            AttrAttrgroupRelationEntity relation = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().lambda().eq(AttrAttrgroupRelationEntity::getAttrId, t.getAttrId()));
            if (Objects.nonNull(relation) && relation.getId() != null) {
                relationIds.add(relation.getId());
            }
        });
        relationDao.deleteBatchIds(relationIds);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupVo> getAttrGroupWithattr(Long catelogId) {
        //通过分类id找到分组
        List<AttrGroupEntity> attrGroupEntities = this.lambdaQuery().eq(AttrGroupEntity::getCatelogId, catelogId).list();
        List<AttrGroupVo> collect = attrGroupEntities.stream().map(t -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(t, attrGroupVo);
            if (t.getAttrGroupId() != null) {
                Long attrGroupId = t.getAttrGroupId();
                //通过分组id找到属性id
                List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>()
                        .lambda().eq(AttrAttrgroupRelationEntity::getAttrGroupId, t.getAttrGroupId()));
                List<Object> attrIds = entities.stream().map(AttrAttrgroupRelationEntity::getAttrId).collect(Collectors.toList());
                if (attrIds.size() > 0) {
                    //找到实体类
                    List<AttrEntity> attrEntities = attrDao.selectList(new QueryWrapper<AttrEntity>()
                            .lambda().in(AttrEntity::getAttrId, attrIds));
                    //为vo赋值
                    List<AttrEntity> attrEntityList = attrEntities.stream().map(attrEntity -> {
                        attrEntity.setAttrGroupId(t.getAttrGroupId());
                        return attrEntity;
                    }).collect(Collectors.toList());
                    attrGroupVo.setAttrs(attrEntityList);
                }
            }
            return attrGroupVo;
        }).filter(t -> t.getAttrs() != null && !(t.getAttrs()).isEmpty()).collect(Collectors.toList());
        return collect;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        LambdaQueryWrapper<AttrGroupEntity> wrapper = getQueryWrapper(params);
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        } else {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper.eq(AttrGroupEntity::getCatelogId, catelogId)
            );
            return new PageUtils(page);
        }
    }

    private LambdaQueryWrapper<AttrGroupEntity> getQueryWrapper(Map<String, Object> params) {
        String key = (String) params.get("key");
        if (Objects.nonNull(key)) {
            return new QueryWrapper<AttrGroupEntity>().lambda().and(t -> {
                t.like(AttrGroupEntity::getAttrGroupId, key).or()
                        .like(AttrGroupEntity::getAttrGroupName, key);
            });
        } else {
            return new QueryWrapper<AttrGroupEntity>().lambda();
        }
    }


}